
import sql from '@/utils/sqlLite.js'
import {
	setObjSql,
	obj2str_sql
} from '@/utils/index.js'
// 数据表名称  发送密码生成一条记录

export const passConfig = 'passConfig'
export const database = uni.getStorageSync('database')
/*

			"chargeUnit":"0",
			"counter":"30000",//计数器
			"createTime":"2024-08-20 17:50:20",
			"days":"123",解锁天数
			"keycode":"934jrefoi34u",密码
			"pid":"239623829",
			"rentCode":"RT20240731lDddd9a7B",
			"secKey":"98349",productSecrlt
			"sendStatus":"0",发送状态 0 成功 1失败
			"unlockStatus":"0"解锁状态
*/
export const cratePassConfig = async () => {
	let sqltxt = [
		`create table if not exists ${passConfig}("chargeUnit" init(1),"counter" VARCHAR(6),"createTime" VARCHAR(24),
		"days" VARCHAR(2),"keycode" VARCHAR(16),"pid" VARCHAR(16),"rentCode" VARCHAR(16),"secKey" VARCHAR(16),"sendStatus" VARCHAR(1),"unlockStatus" VARCHAR(1))`,
	];
	let res = await sql.executeSQL(uni.getStorageSync('database'), sqltxt);
	console.log(res);
}

// 添加记录
export const addPassConfig = async (pushData) => {

	// 数据库中rentCode 存在 修改 ，不存在 新增
	let sqltxt = `select *from ${passConfig} where rentCode = '${pushData.rentCode}'`;
	let res = await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
	console.log(res);
	if (res.code == 0 && res.data.length == 0) { //数据库中没有数据 新增

		let condition = Object.keys(pushData).join(',')
		let data = setObjSql(pushData)
		console.log(condition,data);
		
		let sqltxt = `INSERT  INTO ${passConfig} (${condition}) VALUES(${data})`
		return await sql.executeSQL(uni.getStorageSync('database'), sqltxt);

	} else { //存在 ，更改
		let data = obj2str_sql(pushData)
		let sqltxt = `update  ${passConfig} set ${data} where rentCode = '${pushData.rentCode}'`
		let rrs =  await sql.executeSQL(uni.getStorageSync('database'), sqltxt);
		console.log(rrs);
	}
}

// 获取全部密码记录
export const getPassConfig = async () => {
	let sqltxt = `select * from ${passConfig}`;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
}
